﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCarERP.Models;

namespace WebCarERP.Controllers
{
    public class PersonInfoController : BaseController
    {
        //
        // GET: /PersonInfo/

        public ActionResult Index()
        {
            ViewBag.Title = "员工信息";
         var info=from x in    db.SamplePersonInfo.AsEnumerable().Where(t=>t.Flag==1)
                  join y in db.SamplePersonInfoItem.AsEnumerable().Where(t => t.Flag == 1) 
                          on x.InfoId equals y.InfoId
                  select new SamplePersonInfoEntity{ 
                      InfoId=x.InfoId ,
                      Name=x.Name,
                      Mobile = x.Mobile,
                      MoreMobile = x.MoreMobile,
                      Sex = x.Sex,
                      Memo = x.Memo,
                      StartDate = y.StartDate,
                      EndDate = y.EndDate,
                      Birthday = x.Birthday,
                  };
            return View(info.OrderByDescending(t=>t.InfoId));
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult add(SamplePersonInfo entity,SamplePersonInfoItem entity2) {
            entity.Flag = 1;

            db.SamplePersonInfo.Add(entity);
            int resultCount = db.SaveChanges();
            entity2.InfoId = entity.InfoId;
            entity2.Flag = 1;
            db.SamplePersonInfoItem.Add(entity2);
             resultCount = db.SaveChanges();
            var info=from x in    db.SamplePersonInfo.AsEnumerable().Where(t=>t.InfoId==entity.InfoId) 
                          join y in db.SamplePersonInfoItem.AsEnumerable().Where(t=>t.InfoId==entity.InfoId) 
                          on x.InfoId equals y.InfoId
                  select new SamplePersonInfoEntity{ 
                      InfoId=x.InfoId ,
                      Name=x.Name,
                      Mobile = x.Mobile,
                      MoreMobile = x.MoreMobile,
                      Sex = x.Sex,
                      Memo = x.Memo,
                      StartDate = y.StartDate,
                      EndDate = y.EndDate,
                      Birthday = x.Birthday,
                  };
            string json = JsonConvert.SerializeObject(info.FirstOrDefault());
            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"添加成功\",\"data\":[" + json + "]}");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult edit(SamplePersonInfo entity, SamplePersonInfoItem entity2)
        {
            //entity.Flag = 1;

            //db.SamplePersonInfo.Add(entity);
            //int resultCount = db.SaveChanges();
            //entity2.InfoId = entity.InfoId;
            //entity2.Flag = 1;
            //db.SamplePersonInfoItem.Add(entity2);
            //resultCount = db.SaveChanges();
            SamplePersonInfo SamplePersonInfo = db.SamplePersonInfo.Find(entity.InfoId);
            SamplePersonInfo.Name = entity.Name;
            SamplePersonInfo.Mobile = entity.Mobile;
            SamplePersonInfo.MoreMobile = entity.MoreMobile;
            SamplePersonInfo.Sex = entity.Sex;
            SamplePersonInfo.Birthday = entity.Birthday;
            SamplePersonInfo.Memo = entity.Memo;
            db.Entry(SamplePersonInfo).State = EntityState.Modified;

            SamplePersonInfoItem SamplePersonInfoItem = db.SamplePersonInfoItem.Where(t=>t.InfoId==entity.InfoId).First();
            SamplePersonInfoItem.StartDate = entity2.StartDate;
            SamplePersonInfoItem.EndDate = entity2.EndDate;
            db.Entry(SamplePersonInfoItem).State = EntityState.Modified;
            int resultCount = db.SaveChanges();
            var info = from x in db.SamplePersonInfo.AsEnumerable().Where(t => t.InfoId == entity.InfoId)
                       join y in db.SamplePersonInfoItem.AsEnumerable().Where(t => t.InfoId == entity.InfoId)
                       on x.InfoId equals y.InfoId
                       select new SamplePersonInfoEntity
                       {
                           InfoId = x.InfoId,
                           Name = x.Name,
                           Mobile = x.Mobile,
                           MoreMobile = x.MoreMobile,
                           Sex = x.Sex,
                           Memo = x.Memo,
                           StartDate = y.StartDate,
                           EndDate = y.EndDate,
                           Birthday = x.Birthday,
                       };
            string json = JsonConvert.SerializeObject(info.FirstOrDefault());
            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"修改成功\",\"data\":[" + json + "]}");
        }

           [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult delete(int? InfoId)
        {
   
            SamplePersonInfo SamplePersonInfo = db.SamplePersonInfo.Find(InfoId);
            SamplePersonInfo.Flag = 0;
            db.Entry(SamplePersonInfo).State = EntityState.Modified;

            SamplePersonInfoItem SamplePersonInfoItem = db.SamplePersonInfoItem.Where(t=>t.InfoId==InfoId).First();
            SamplePersonInfoItem.Flag = 0;
            db.Entry(SamplePersonInfoItem).State = EntityState.Modified;
            int resultCount = db.SaveChanges();
       
            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"删除成功！\",\"data\":[]}");
        }
        
    }
}
